/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.search.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.thinkgem.jeesite.modules.search.core.AndmobiSearcher;
import com.thinkgem.jeesite.modules.search.core.pojo.SearchResult;
import com.thinkgem.jeesite.modules.search.core.service.SearchService;
import com.thinkgem.jeesite.modules.search.entity.SearchArticle;

/**
 * 文章的全文索引Service
 * 
 * @author hongye
 * @version 2017-04-19
 */
@Service
public class SearchArticleService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	private SearchService service = AndmobiSearcher.getService();

	public void createOrUpdateIndex(List<SearchArticle> list) {
		if (list == null || list.isEmpty()) {
			return;
		}

		try {
			service.createOrUpdateIndexBatch(list);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			e.printStackTrace();
		}
	}

	public List<SearchArticle> searchByKeyword(String keyword) {
		List<SearchArticle> retList = new ArrayList<SearchArticle>();
		SearchResult<SearchArticle> result = new SearchResult<SearchArticle>();
		try {
			result = service.findSearchResult(SearchArticle.class, keyword);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
			e.printStackTrace();
		}
		logger.debug("result size======>" + result.getSize());
		for (SearchArticle t : result.getResult()) {
			retList.add(t);
		}

		return retList;
	}

}